package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.pojo.TbParameter;
import com.workManager.pojo.TbParameterType;
import com.workManager.pojo.TdRole;
import com.workManager.service.ParameterService;
import com.workManager.service.ParameterServiceImpl;
import com.workManager.vo.ParameterTypeVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/2/24 0024.
 */
@Controller
@RequestMapping("/parameter")
public class ParameterController extends BaseController {


    @Autowired
    private ParameterService parameterService;

    @ApiOperation(value ="参数查询",notes = "根据参数类型查询参数")
    @RequestMapping(value = "/queryByParameterType",method = RequestMethod.POST)
    /*@RequiresPermissions(value = "/parameter/queryByParameterType")*/
    public Object queryByParameterType(HttpServletRequest request, @RequestParam(value="parameterType",required = false) String parameterType){
        ModelMap modelMap = new ModelMap();

        List<TbParameter>  list = parameterService.queryByParameterType(parameterType);
        modelMap.put("list",list);

        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="参数更新",notes = "更新参数")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(HttpServletRequest request, @RequestBody TbParameter parameter ){
        ModelMap modelMap = new ModelMap();
        /*Map<String,Object> map = new HashMap<String, Object>();
        map.put("id",parameter.getParamId());
        PageInfo<TbParameter> parameterPageInfo = parameterService.queryParameter(map);
        TbParameter parameter1 = parameterPageInfo.getList().get(0);
        //校验要更新的名字和现有的有没有冲突
        if(parameter1!=null&&parameter.getParamName()!=null&&!parameter.getParamName().equals(parameter1.getParamName())){
            Map<String,Object> map2 = new HashMap<String, Object>();
            map2.put("name",parameter.getParamName());
            PageInfo<TbParameter>  parameterPageInfo1 = parameterService.queryParameter(map2);
            if(parameterPageInfo1.getTotal()>0){
                return  setModelMap(modelMap, RespCode.OK,"参数名："+parameter.getParamName()+"已存在！");
            }
        }*/

        try{
            parameterService.update(parameter);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"参数名："+parameter.getParamName()+"已存在！");
        }




        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="参数类型更新",notes = "更新参数类型")
    @RequestMapping(value = "/updateParameterType",method = RequestMethod.POST)
    public Object updateParameterType(HttpServletRequest request, @RequestBody TbParameterType parameterType ){
        ModelMap modelMap = new ModelMap();

        try {
            parameterService.updateParameterType(parameterType);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"参数类型名称："+parameterType.getParamTypeName()+" 或参数值："+parameterType.getParamTypeValue()+" 已存在！");
        }



        return  setSuccessModelMap(modelMap);
    }




    @ApiOperation(value ="参数类型查询",notes = "查询参数的类型")
    @RequestMapping(value = "/queryParameterType",method = RequestMethod.POST)
    public Object queryParameterType(@RequestParam(value="parameterTypeName", required = false) String parameterTypeName,
                                     @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                     @RequestParam(value="pageSize", required = false) String pageSize){
        ModelMap modelMap = new ModelMap();

        Map<String,Object> map = new HashMap<String, Object>();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);

        Map parammap = new HashMap();
        parammap.put("pagenum",pagenum);
        parammap.put("pagesize",pagesize);
        parammap.put("parameterTypeName",parameterTypeName);

        PageInfo<ParameterTypeVo> list = parameterService.queryParameterType2(parammap);
        map.put("total",list.getTotal());
        map.put("rows",list.getList());

        return  setSuccessModelMap(modelMap,map);
    }



    @ApiOperation(value ="参数类型添加",notes = "参数类型添加")
    @RequestMapping(value = "/insertParameterType",method = RequestMethod.POST)
    public Object insertParameterType(HttpServletRequest request, @RequestBody TbParameterType parameterType){
        ModelMap modelMap = new ModelMap();
        parameterType.setCreateDate(new Date());
        parameterType.setIsValid(true);
        try {
            parameterService.insertParameterType(parameterType);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"参数类型名称："+parameterType.getParamTypeName()+" 或参数值："+parameterType.getParamTypeValue()+" 已存在！");
        }

        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="参数类型删除",notes = "参数类型删除")
    @RequestMapping(value = "/deleteParameterType",method = RequestMethod.POST)
    public Object deleteParameterType(HttpServletRequest request,  @RequestParam(value="typeIds", required = true) List<String> typeIds){
        ModelMap modelMap = new ModelMap();

        try {
            parameterService.deleteParameterType(typeIds);
        }catch (DataIntegrityViolationException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"该类型对应相关参数，不能删除！");
        }

        return  setSuccessModelMap(modelMap);
    }



    @ApiOperation(value ="参数添加",notes = "参数添加")
    @RequestMapping(value = "/insertParameter",method = RequestMethod.POST)
    public Object insertParameter(HttpServletRequest request, @RequestBody TbParameter parameter){
        ModelMap modelMap = new ModelMap();
        parameter.setCreateDate(new Date());
        parameter.setIsValid(true);

        /*Map<String,Object> map = new HashMap<String, Object>();
        map.put("name",parameter.getParamName());
        PageInfo<TbParameter> parameterPageInfo = parameterService.queryParameter(map);
        if(parameterPageInfo.getTotal()>0){
            return  setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR,"参数名："+parameter.getParamName()+" 已存在！");
        }*/

        try{
            parameterService.insertParameter(parameter);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"参数名:"+parameter.getParamName()+" 已存在！");
        }


        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="参数删除",notes = "参数删除")
    @RequestMapping(value = "/deleteParameter",method = RequestMethod.POST)
    public Object deleteParameter(@RequestParam(value="id", required = true) String id){
        ModelMap modelMap = new ModelMap();
        parameterService.deleteParameter(id);
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="参数批量删除",notes = "参数批量删除")
    @RequestMapping(value = "/deleteParameterByList",method = RequestMethod.POST)
    public Object deleteParameterByList(@RequestParam(value="ids", required = true) List<String> ids){
        ModelMap modelMap = new ModelMap();
        parameterService.deleteParameterByList(ids);
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="参数查询",notes = "参数查询")
    @RequestMapping(value = "/queryParameter",method = RequestMethod.POST)
    public Object queryParameter(HttpServletRequest request,  @RequestParam(value="name", required = true) String name){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name",name);
        PageInfo<TbParameter> parameterPageInfo  = parameterService.queryParameter(map);
        modelMap.put("list",parameterPageInfo.getList());
        return  setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="子参数类型查询",notes = "子参数类型查询")
    @RequestMapping(value = "/queryChildParameterType",method = RequestMethod.POST)
    public Object queryChildParameterType(@RequestParam(value="paramTypeName", required = true) String paramTypeName){
        List<TbParameterType> directExpensesCategoryList =  parameterService.getChildrenByName(paramTypeName);
        ModelMap modelMap = new ModelMap();
        return  setSuccessModelMap(modelMap,directExpensesCategoryList);
    }

}
